---
title: 'Replication file: Negative Political Identities and Costly Political Action'
author: "Katharina Lawall, Stuart J Turnbull-Dugarte, Florian Foos \& Josh Townsley"
date: "`r format(Sys.time(), '%B %d, %Y')`"
output:
  pdf_document: default
  bookdown::pdf_document2:
    toc: false
    keep_tex: yes
    latex_engine: xelatex
abstract:
  
keywords: 
mainfont: cochineal
fontsize: 11pt
---


<!-- Set your working directory here -->

```{r setup, include=FALSE, cache = FALSE}
#install.packages("knitr")
rm(list=ls())
require("knitr")
#knitr::opts_knit$set(root.dir = "C:/Users/katharina.lawall/Downloads/NegativePoliticalIdentities")



```


<!--Load all the required libraries in -->

```{r libraries, include=FALSE, cache = FALSE}

library(margins)

library(dplyr)
library(tidyr)

library(ggplot2)
library(stringr)


library(estimatr)



library(ggpubr)




```

<!--Loading in data -->

```{r dataload, , echo=FALSE}


data <- read.csv("data/fieldexpdata.csv") 

surveydata <- read.csv("data/surveydata.csv")
```




# Manuscript, Figure 2: Manipulation check - Who is meant by the we in the email?



```{r fig2, echo=F, warning=F}

# get proportions of answer categories by treatment condition with confidence intervals

plot <- surveydata %>%
  group_by(treatment_cat, we_id) %>%
  summarise(n = n()) %>%
  mutate(prop = n/sum(n),
         upperE=1.96*sqrt(n/sum(n)*(1-(n/sum(n)))/sum(n)), 
         lowerE=-1.96*sqrt(n/sum(n)*(1-(n/sum(n)))/sum(n)),
         ymin = prop+lowerE, 
         ymax = prop+upperE,
         ymin= replace(ymin, ymin<0, 0))


# plot proportions by treatment condition

ggplot(plot, aes(x=treatment_cat, y = prop, fill = we_id, pattern=we_id))+ 
  geom_bar(position = "dodge", stat = "identity") + geom_errorbar(aes(ymin = ymin, ymax = ymax),width = .2, position=position_dodge(.9))+
  labs(x="Treatment condition",y="Proportion of respondents") +
  ylim(0,1) +
  ggtitle("Who do you think is mainly meant by the “we” in the email?")+
  theme_minimal() +
scale_x_discrete(guide = guide_axis(n.dodge = 2)) +
  scale_fill_manual(values = alpha(c("#ffffbf", '#21719e', '#abd9e9',"#fdae61","#0b0a0a"), 1 ), name="People who identify as...") +
  theme(
plot.title = element_text(hjust = 0, size = 12, face="bold"),
axis.title.y = element_text(size=12),
axis.title.x = element_text(size=12),
axis.text.x = element_text(size=12),
axis.text.y =element_text(size=12),
legend.text = element_text(size = 12),
strip.text.x = element_text(size = 12),
legend.background = element_blank(),
legend.box.background = element_rect(colour = "grey"),
legend.title=element_text(size=12)
) 



# save figure 
ggsave("output/fig2_manip.png",width =24, height = 19, units = "cm", dpi=320)


```




# Manuscript, Figure 3: Mean probability of donating by experimental condition

```{r mainplots_fig3, echo=F}




# get mean values and CI
summarystats <-
  data %>% group_by(treatment_cat) %>%
  do(tidy(lm_robust(donor7days ~ 1, data = .))) %>%
  mutate(predicted = estimate) %>%
  mutate(conf.low.83 = estimate -(std.error*1.3722),
                          conf.high.83 = estimate + (std.error*1.3722))


# make figure 3
ggplot(data, aes(x=treatment_cat, y=predicted, color=treatment_cat)) +
  theme_minimal() +
  geom_point(data = summarystats, size = 2) +
  geom_errorbar(data = summarystats,
                aes(ymin = conf.low, ymax = conf.high), colour= "black", width=0, size=0.9, alpha=0.5) +
  geom_linerange(data=summarystats, aes(ymin=conf.low.83, ymax=conf.high.83), width=.3, size=1.5, alpha=1.5) +
  theme(
plot.title = element_text(hjust = 0, size = 14, face="bold"),
axis.title.y = element_text(size=12),
axis.title.x = element_blank(),
axis.text.x = element_text(size=12),
axis.text.y =element_text(size=12),
legend.text = element_text(size = 9),
strip.text.x = element_text(size = 12),
legend.position = "none",
legend.background = element_blank(),
legend.box.background = element_rect(colour = "grey"),
legend.title=element_text(size=9)
) +
    labs(x = NULL, y = "Pr(donation) 7 days post-treatment" , title="Mean probability of donating by experimental condition", caption = "83% CI (thick) and 95% CI (thin), p<0.05=**, p<0.1=*")+
    ylim(0.01,0.03)  +
  scale_color_manual(values=c('#999999','#2a94cf','#21719e', "#f1c232", "#e69138"))+
  scale_size_manual(values=c(2,3,4)) +
scale_x_discrete(guide = guide_axis(n.dodge = 2))+
  geom_text(aes(x = 1.5,  y = 0.03, label = "\u03b2=0.003*")) +
  geom_segment(aes(x = 1, xend = 1, 
           y = 0.028, yend = 0.029),
           colour = "black") +
  geom_segment(aes(x = 2, xend = 2, 
           y = 0.028, yend = 0.029),
           colour = "black") +
  geom_segment(aes(x = 1, xend = 2, 
           y = 0.029, yend = 0.029),
           colour = "black")


# save figure
ggsave("output/fig3_allgroups.png",width =24, height = 19, units = "cm", dpi=320)

```

# Manuscript, Figure 4: Negative vs positive cue

```{r mainplots_fig4, echo=F}


#mean values and CI
summarystats <-
  data %>% group_by(treatment_cat_negative) %>%
  do(tidy(lm_robust(donor7days~ 1, data = .))) %>%
  mutate(predicted = estimate) %>%
  mutate(conf.low.83 = estimate -(std.error*1.3722),
                          conf.high.83 = estimate + (std.error*1.3722))


# make panel i) of Figure 4
g1 <- 
ggplot(data, aes(x=treatment_cat_negative, y=predicted, color=treatment_cat_negative)) +
  theme_minimal() +
  geom_point(data = summarystats, size = 2) +
  geom_errorbar(data = summarystats,
                aes(ymin = conf.low, ymax = conf.high), colour= "black", width=0, size=0.9, alpha=0.5) +
  geom_linerange(data=summarystats, aes(ymin=conf.low.83, ymax=conf.high.83), width=.3, size=1.5, alpha=1.5) +
  theme(
plot.title = element_text(hjust = 0, size = 12, face="bold"),
axis.title.y = element_text(size=11),
axis.title.x = element_blank(),
axis.text.x = element_text(size=11),
axis.text.y =element_text(size=11),
legend.text = element_text(size = 9),
strip.text.x = element_text(size = 11),
legend.position = "none",
legend.background = element_blank(),
legend.box.background = element_rect(colour = "grey"),
legend.title=element_text(size=9)
) +
    labs(x = NULL, y = "Pr(donation at 7 days post-treatment)" , title="i) Mean probability of donating \nby experimental condition", caption = "83% CI (thick) and 95% CI (thin)")+
    ylim(0.015,0.025)  +
  scale_shape_manual(values=c(19, 17, 18))+
  scale_color_manual(values=c('#999999','#56B4E9', '#E69F00'))+
  scale_size_manual(values=c(2,3,4))

```


```{r plot, echo=F}

# make panel ii) of Figure 4: marginal effects plot

# Get marginal effects for 2 days, 7 days and 14 days post-treatment

# Create an empty list 
gg_df_list <- list()


for (days in c(2,7,14)) {
  # Create the formula for each outcome
  formula <- as.formula(paste0("donor", days, "days", "~ treat_negative"))
  
  # Run linear regression model
  modelX <- lm_robust(formula, data = data)
  
  # Compute marginal effect 
  gg_df <- modelX %>%
    margins() %>%
    summary() %>%
    as.data.frame() %>%
    mutate(days = days)
  
  # Append the result for  each outcome to the list
  gg_df_list[[days]] <- gg_df
}

# Combine the results into one dataframe

gg_df_com <- bind_rows(gg_df_list) %>%
  mutate(days = paste(days, "days"),
    days_f = factor(days, levels = c("2 days", "7 days", "14 days")))

# Plot the results
g2<- ggplot(gg_df_com, aes(x=days_f, y=AME)) +
  theme_minimal() +
  geom_point(size = 1) +
  geom_linerange( aes(ymin=lower, ymax=upper)) +
  theme(
plot.title = element_text(hjust = 0, size = 12, face="bold"),
axis.title.y = element_text(size=11),
axis.title.x = element_blank(),
axis.text.x = element_text(size=11),
axis.text.y =element_text(size=11),
legend.text = element_text(size = 9),
strip.text.x = element_text(size = 11),
legend.position = "none",
legend.background = element_blank(),
legend.box.background = element_rect(colour = "grey"),
legend.title=element_text(size=9)
) +
    labs(x = "Days since treatment email", y = NULL, title="ii) Marginal effect of negative vs positive cue \non probability of donating") +
    ylim(-0.005,0.009)  +
  scale_shape_manual(values=c(19, 17, 18))+
  scale_color_manual(values=c('#999999','#56B4E9', '#E69F00'))+
  scale_size_manual(values=c(2,3,4)) +
    geom_hline(yintercept=0, color="red") 

# combine plots into one figure
p3 <- ggarrange(g1, g2, ncol=2, nrow=1, widths = c(1.1, 1), align="hv")

p3

ggsave("output/fig4_negativevspositive.png",width =24, height = 19, units = "cm", dpi=320)




```





# Manuscript, Figure 5: Issue vs party cue

```{r mainplots_fig5a, echo=F}


#get mean values and CI
summarystats <-
  data %>% group_by(treatment_cat_issue) %>%
  do(tidy(lm_robust(donor7days~ 1, data = .))) %>%
  mutate(predicted = estimate) %>%
  mutate(conf.low.83 = estimate -(std.error*1.3722),
                          conf.high.83 = estimate + (std.error*1.3722))


# plot mean values and CI
g1 <- 
ggplot(data, aes(x=treatment_cat_issue, y=predicted, color=treatment_cat_issue)) +
  theme_minimal() +
  geom_point(data = summarystats, size = 2) +
  geom_errorbar(data = summarystats,
                aes(ymin = conf.low, ymax = conf.high), colour= "black", width=0, size=0.9, alpha=0.5) +
  geom_linerange(data=summarystats, aes(ymin=conf.low.83, ymax=conf.high.83), width=.3, size=1.5, alpha=1.5) +
  theme(
plot.title = element_text(hjust = 0, size = 12, face="bold"),
axis.title.y = element_text(size=11),
axis.title.x = element_blank(),
axis.text.x = element_text(size=11),
axis.text.y =element_text(size=11),
legend.text = element_text(size = 9),
strip.text.x = element_text(size = 11),
legend.position = "none",
legend.background = element_blank(),
legend.box.background = element_rect(colour = "grey"),
legend.title=element_text(size=9)
) +
    labs(x = NULL, y = "Pr(donation at 7 days post-treatment)" , title="i) Mean probability of donating \nby experimental condition", caption = "83% CI (thick) and 95% CI (thin)")+
    ylim(0.015,0.025)  +
  scale_shape_manual(values=c(19, 17, 18))+
  scale_color_manual(values=c('#999999','#56B4E9', '#E69F00'))+
  scale_size_manual(values=c(2,3,4))

```


```{r mainplots_fig5b, echo=F}

# panel ii) marginal effects plot
# Get marginal effects for 2 days, 7 days and 14 days post-treatment

# Create an empty list 
gg_df_list <- list()


for (days in c(2,7,14)) {
  # Create the formula for each outcome
  formula <- as.formula(paste0("donor", days, "days", "~ treat_issue"))
  
  # Run linear regression model
  modelX <- lm_robust(formula, data = data)
  
  # Compute marginal effect 
  gg_df <- modelX %>%
    margins() %>%
    summary() %>%
    as.data.frame() %>%
    mutate(days = days)
  
  # Append the result for  each outcome to the list
  gg_df_list[[days]] <- gg_df
}

# Combine the results into one dataframe

gg_df_com <- bind_rows(gg_df_list) %>%
  mutate(days = paste(days, "days"),
    days_f = factor(days, levels = c("2 days", "7 days", "14 days")))

g2<- ggplot(gg_df_com, aes(x=days_f, y=AME)) +
  theme_minimal() +
  geom_point(size = 1) +
  geom_linerange( aes(ymin=lower, ymax=upper)) +
  theme(
plot.title = element_text(hjust = 0, size = 12, face="bold"),
axis.title.y = element_text(size=11),
axis.title.x = element_blank(),
axis.text.x = element_text(size=11),
axis.text.y =element_text(size=11),
legend.text = element_text(size = 9),
strip.text.x = element_text(size = 11),
legend.position = "none",
legend.background = element_blank(),
legend.box.background = element_rect(colour = "grey"),
legend.title=element_text(size=9)
) +
    labs(x = "Days since treatment email", y = NULL, title="ii) Marginal effect of issue vs party cue \non probability of donating") +
    ylim(-0.005,0.009)  +
  scale_shape_manual(values=c(19, 17, 18))+
  scale_color_manual(values=c('#999999','#56B4E9', '#E69F00'))+
  scale_size_manual(values=c(2,3,4)) +
    geom_hline(yintercept=0, color="red") 

# combine plots

p3 <- ggarrange(g1, g2, ncol=2, nrow=1, widths = c(1.1, 1), align="hv")

p3

ggsave("output/fig5_issuevsparty.png",width =24, height = 19, units = "cm", dpi=320)

```






# Manuscript, Figure 6, interaction plot: negative vs positive cue conditional on issue treatment


```{r mainplots_fig6, echo=F}

modelX<-lm_robust(donor7days ~ treat_negative + treat_issue + treat_negative*treat_issue, data=data)

gg_df <-
  modelX %>%
  margins(at = list(treat_issue = 0:1)) %>%
  summary %>%
  as.data.frame() 

gg_df1 <- gg_df %>% filter(factor=="treat_negative") %>%
  mutate(treat_issuecat = case_when(treat_issue==0 ~ "Party cue",
                                    treat_issue==1 ~ "Issue cue"))


g2<- ggplot(gg_df1, aes(x=treat_issuecat, y=AME)) +
  theme_minimal() +
  geom_point(size = 1) +
  geom_linerange( aes(ymin=lower, ymax=upper)) +
  theme(
plot.title = element_text(hjust = 0, size = 14, face="bold"),
axis.title.y = element_text(size=12),
axis.title.x = element_blank(),
axis.text.x = element_text(size=12),
axis.text.y =element_text(size=12),
legend.text = element_text(size = 9),
strip.text.x = element_text(size = 12),
legend.position = "none",
legend.background = element_blank(),
legend.box.background = element_rect(colour = "grey"),
legend.title=element_text(size=9)
) +
    labs(x = NULL, y = "Effect on Pr(donation) 7 days post-treatment" , title="Marginal effect of negative vs positive cue on probability of donating \nconditional on issue identity cue", caption= "p<0.01=***, p<0.05=**, p<0.1=*") +
   ylim(-0.005,0.012)  +
  scale_shape_manual(values=c(19, 17, 18))+
  scale_color_manual(values=c('#999999','#56B4E9', '#E69F00'))+
  scale_size_manual(values=c(2,3,4)) +
    geom_hline(yintercept=0, color="red") +
  geom_text(aes(x = 1.5,  y = 0.011, label = "\u03b2=0.005**")) +
  geom_segment(aes(x = 1, xend = 1, 
           y = 0.009, yend = 0.01),
           colour = "black") +
  geom_segment(aes(x = 2, xend = 2, 
           y = 0.009, yend = 0.01),
           colour = "black") +
  geom_segment(aes(x = 1, xend = 2, 
           y = 0.01, yend = 0.01),
           colour = "black")

g2

ggsave("output/fig6_negativeinteract.png",width =24, height = 19, units = "cm", dpi=320)

```

# Manuscript, Figure 7: ITT of identity cue emails on daily donations

```{r mainplots_fig7, echo=F}

margins.func <- function(outcome,n){
  
lm1 <- lm_robust((eval(parse(text=paste(outcome, sep="")))) ~ treatment_cat,  data=data)

m<- margins(lm1) %>%
  summary %>%
  as.data.frame() %>%
  mutate (days=n)



return(m)
}


allm_control <- data.frame()
for (i in 1:14) {
  outcome <- paste0("donor", i, "days")
  m <- margins.func(outcome, i)
  allm_control <- rbind(allm_control, m)
}

allm_control_pret <- data.frame()
for (i in 1:7) {
  outcome <- paste0("pret_donor", i, "days")
  m <- margins.func(outcome, i)
  allm_control_pret <- rbind(allm_control_pret, m)
}
allm_control_pret <- allm_control_pret %>% mutate(days= 0-days)

allm_combined_control <- rbind(allm_control, allm_control_pret)



allm_combined_control <- allm_combined_control %>% mutate (factorname = str_replace(factor, "treatment_cat", ""))


allm_combined_control %>%
  mutate(colorgroup = case_when(days<0 ~ "light",
                                days>0  ~ "middle"))%>%
ggplot(., aes(days, AME, color=colorgroup)) +
  geom_point(size=2) +
  geom_errorbar(aes(ymax = lower, ymin = upper), size= 1,width = 0) +
  geom_hline(yintercept = 0, linetype = "dashed", colour="#205C8A") +
  xlab("Days since treatment") +
  ylab("") +
  theme_minimal() +
  labs(title="Marginal effect of political identity cues (vs Control) on Pr (donation)") +
  facet_wrap(~factorname)+
  theme(
plot.title = element_text(hjust = 0, size = 12, face="bold"),
axis.title.y = element_text(size=11),
axis.title.x = element_text(size=11),
axis.text.x = element_text(size=11),
axis.text.y =element_text(size=11),
legend.text = element_text(size = 9),
strip.text.x = element_text(size = 11),
legend.position = "none",
legend.background = element_blank(),
legend.box.background = element_rect(colour = "grey"),
legend.title=element_text(size=9)
) +
 scale_x_continuous(breaks=seq(-7,14,2))+
  geom_vline(xintercept = 0, linetype = "solid", colour="red")+
  scale_color_manual(values=c( "#969696", "#0b0a0a" ))



ggsave("output/fig7_overtime_itt_tvscontrol.png",width =24, height = 19, units = "cm", dpi=320)

```

